/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cardgame.game;

import java.io.IOException;
import cardgame.network.*;

/**
 *
 * @author Carl
 */
public class MonsterCard extends Card{

    public MonsterCard(){
        
    }
    protected Cost summonCost = new Cost();
    protected int attackDamage;
    protected int maximumLifepoints;
    protected int currentLifepoints;
    protected Cost attackCost = new Cost(new ManaAmount(), 0, true);
    private Spell[] spells = new Spell[0];
    
    public void onSummon(){
        
    }
    
    public void onDeath(){
        
    }

    public void setSummonCost(Cost summonCost){
        this.summonCost = summonCost;
    }

    public Cost getSummonCost(){
        return summonCost;
    }

    public int getAttackDamage(){
        return attackDamage;
    }
    
    public void setLifepoints(int lifepoints){
        maximumLifepoints = lifepoints;
        currentLifepoints = lifepoints;
    }

    public int getMaximumLifepoints(){
        return maximumLifepoints;
    }

    public void setMaximumLifepoints(int maximumLifepoints){
        int difference = (maximumLifepoints - this.maximumLifepoints);
        this.maximumLifepoints = maximumLifepoints;
        if(difference > 0){
            currentLifepoints += difference;
        }
        else if(currentLifepoints > maximumLifepoints){
            currentLifepoints = maximumLifepoints;
        }
    }

    public void setCurrentLifepoints(int currentLifepoints){
        if(currentLifepoints < 0){
            currentLifepoints = 0;
        }
        else if(currentLifepoints > maximumLifepoints){
            currentLifepoints = maximumLifepoints;
        }
        this.currentLifepoints = currentLifepoints;
    }

    public int getCurrentLifepoints(){
        return currentLifepoints;
    }
    
    public boolean canAttack(){
        return owner.canPay(attackCost, this);
    }

    public void setSpells(Spell[] spells){
        for(Spell spell : spells){
            spell.setCaster(this);
        }
        this.spells = spells;
    }

    public Cost getAttackCost(){
        return attackCost;
    }

    public Spell[] getSpells(){
        return spells;
    }

    @Override
    public void write(BitOutputStream outputStream){
        super.write(outputStream);
        outputStream.writeBits(maximumLifepoints, 8);
        outputStream.writeBits(currentLifepoints, 8);
        outputStream.writeBits(attackDamage, 7);
    }

    @Override
    public void read(BitInputStream inputStream) throws IOException{
        super.read(inputStream);
        maximumLifepoints = inputStream.readBits(8);
        currentLifepoints = inputStream.readBits(8);
        attackDamage = inputStream.readBits(7);
    }
}
